
using System;
using System.Linq;
using Csla;
using Csla.Data;
using HousingInfo.DataLinQ;

namespace HousingInfo.Library
{
    public partial class UserBOList : Csla.BusinessListBase<UserBOList, UserBO>
    {

        #region Factory Methods

        #region Code cho trang User admin

        #region ham lay tat ca danh sach user
        public static UserBOList GetAllUserBOList()
        {
            return DataPortal.Fetch<UserBOList>();
        }
        #endregion

        public static UserBOList GetUserBOListByIsActive(bool isActive)
        {
            return DataPortal.Fetch<UserBOList>(new SingleCriteria<UserBOList, bool>(isActive));
        }
        #endregion

        #region Authorization Rules
        protected static void AddObjectAuthorizationRules()
        {
            //TODO: Define object-level authorization rules of UserBOList
            //AuthorizationRules.AllowCreate(typeof(UserBOList), "UserBOListCreateGroup"));
            //AuthorizationRules.AllowEdit(typeof(UserBOList), "UserBOListEditGroup"));
            //AuthorizationRules.AllowDelete(typeof(UserBOList), "UserBOListDeleteGroup"));
        }
        #endregion //Authorization Rules

        #region Data Access - Fetch

        #region truy cap du lieu cua ham lay tat ca danh sach user
        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Fetch()
        {
            bool cancel = false;
            if (cancel) return;

            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                RaiseListChangedEvents = false;

                this.AddRange(
                    from row in mgr.DataContext.Users
                    orderby row.RegisterDate descending
                    select UserBO.GetUserBO(row.UserName)
                );

                RaiseListChangedEvents = true;
            }//using
        }
        #endregion
        #region Trang cau hoi quen mat khau - Kiem tra cau tra loi con ton tai khi xoa cau hoi
        public static bool CheckQuestionInUsers(int questionID)
        {  
             int count = 0;
            using (var mgr = ContextManager<HousingInfoDataContext>
                    .GetManager(Database.HousingInfoConnection))
            {
                count = mgr.DataContext.Users.Count(n => n.QuestionID == questionID);
            }

            return (count > 0);
        }
        #endregion //Trang cau hoi quen mat khau - Kiem tra cau tra loi con ton tai khi xoa cau hoi
     
        private void DataPortal_Fetch(SingleCriteria<UserBOList, bool> criteria)
        {
            bool cancel = false;
            if (cancel) return;

            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                RaiseListChangedEvents = false;

                this.AddRange(
                    from row in mgr.DataContext.Users
                    where row.IsActive == criteria.Value
                    select UserBO.GetUserBO(row.UserName)
                );

                RaiseListChangedEvents = true;
            }//using


            //OnFetched();
        }
        #endregion
        #endregion

        #region Trang Admin user
        #region Ham xoa tat ca cac lien quan cua user khi xoa user
        #endregion
        #endregion

    }
}